package com.tboss.template.common.excelStyle;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import com.tboss.global.helpers.excel.ExcelDomain;
import com.tboss.global.helpers.excel.StyleHeader;

public class YellowHeader implements StyleHeader {

	@Override
	public void setHeader(Workbook workbook, ExcelDomain<?> domain) {
		workbook.createSheet(domain.getSheetName());//Creating Sheet
		CellStyle cellStyle = this.getCellStyle(workbook);//Applying Style of Sheet
		this.setFont(workbook, cellStyle);//Applying Font of Sheet 
		this.setHeaderRow(workbook, domain, cellStyle);//Writing rows on sheet
	}
	
	private CellStyle getCellStyle(Workbook workbook){
		CellStyle cellStyle = workbook.createCellStyle();		
		cellStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
		cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
		cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
		cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
		cellStyle.setBorderLeft(CellStyle.BORDER_THIN);
		cellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
		cellStyle.setBorderRight(CellStyle.BORDER_THIN);
		cellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
		cellStyle.setBorderTop(CellStyle.BORDER_THIN);
		cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
		cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
		cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
		
		
		return cellStyle;
	}
	
	private void setFont(Workbook workbook, CellStyle cellStyle){
		Font font = workbook.createFont();		
		font.setFontHeightInPoints((short)10);
		font.setBoldweight(Font.BOLDWEIGHT_BOLD);
		font.setColor(IndexedColors.BLACK.getIndex());
		cellStyle.setFont(font);
	}
	
	private void setHeaderRow(Workbook workbook, ExcelDomain<?> domain, CellStyle cellStyle){
		Sheet sheet = workbook.getSheet(domain.getSheetName());
		Row header = sheet.createRow(0);
		header.setHeight((short)500);
		
		int rIdx = 0;
		for(String col : domain.getColumns()){
			Cell hCell = header.createCell(rIdx++);
			hCell.setCellValue(col);
			hCell.setCellStyle(cellStyle);
		}
	}
	
}
